package edu.cebanc.spring.biblioteca.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.cebanc.spring.biblioteca.bean.AuthorBean;
import edu.cebanc.spring.biblioteca.bean.BookBean;
import edu.cebanc.spring.biblioteca.bean.GenreBean;
import edu.cebanc.spring.biblioteca.dao.AuthorsDAO;
import edu.cebanc.spring.biblioteca.dao.BooksDAO;
import edu.cebanc.spring.biblioteca.dao.GenreDAO;
import edu.cebanc.spring.biblioteca.domain.Author;
import edu.cebanc.spring.biblioteca.domain.Book;
import edu.cebanc.spring.biblioteca.domain.Genre;
import edu.cebanc.spring.biblioteca.util.DataParser;

@Service("booksService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class BooksServiceImpl implements BooksService {

	@Autowired
	private BooksDAO booksDAO;
	@Autowired
	AuthorsDAO authorsDAO;
	@Autowired
	GenreDAO genreDAO;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void saveBook(BookBean book) {
		DataParser parser = new DataParser();
		booksDAO.saveBook(parser.BookBean2Book(book));
	}

	@Override
	public List<BookBean> listBooks() {
		DataParser parser = new DataParser();
		return parser.BookList2BookBeanList(booksDAO.listBooks());
	}

	@Override
	public BookBean loadBook(String id) {
		DataParser parser = new DataParser();
		Book b = booksDAO.getBook(Integer.parseInt(id));
		return parser.Book2BookBean(b);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void deleteBook(String id) {
		booksDAO.delBook(booksDAO.getBook(Integer.parseInt(id)));

	}

	@Override
	public List<AuthorBean> getAuthors(BookBean bb) {
		DataParser dp = new DataParser();

		List<Author> al = authorsDAO.listAuthors();
		for (int i = 0; i < al.size(); i++) {
			for (int j = 0; j < bb.getAutores().size(); j++) {
				if (al.get(i).getId_autor() == bb.getAutores().get(j)
						.getId_autor()) {
					al.remove(i);
				}
			}
		}

		return dp.AuthorList2AuthorBeanList(al);
	}

	@Override
	public List<GenreBean> getGenre(BookBean bb) {
		DataParser dp = new DataParser();

		List<Genre> gl = genreDAO.listGenres();
		for (int i = 0; i < gl.size(); i++) {
			for (int j = 0; j < bb.getGeneros().size(); j++) {
				if (gl.get(i).getId_genero() == bb.getGeneros().get(j)
						.getId_genre()) {
					gl.remove(i);
				}
			}
		}

		return dp.GenreList2GenreBeanList(gl);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void addAuthor2Book(BookBean bb, AuthorBean ab) {
		DataParser dp = new DataParser();

		Book book = dp.BookBean2Book(bb);
		book.getAutores().add(authorsDAO.getAuthor(ab.getId_autor()));
		booksDAO.saveBook(book);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public void addGenre2Book(BookBean bb, GenreBean gb) {
		// TODO Auto-generated method stub
		DataParser dp = new DataParser();

		// libro.getGeneros().add(dp.fromGeneroToGeneroBean());
		Book book = dp.BookBean2Book(bb);
		book.getGeneros().add(genreDAO.getGenre(gb.getId_genre()));
		booksDAO.saveBook(book);
	}

}
